package com.fxsh168.server.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * Created by hasee on 2016/11/22.
 */
public class ImgDealUtils {
    static int lost = 0;
   static int dis = 10;
    static int now = 0;
    public static void main(String[] args){
        for (int i = 0; i  < 100; i++){
            if (lost == 0){
                now += Math.abs(lost - dis);
            }else{
                now += Math.abs(lost - dis);
            }
            System.out.println("now=" + now + "\t" + "dis=" + dis + "\t" + "lost=" + lost);
            lost = dis;
            if (i % 2 == 0) {
                dis += 10;
            }else{
                dis += -6;
            }
        }
    }
    public void setImage(View view) {
//        Bitmap thumbnail = createThumbnail(R.mipmap.image_2, 0, 500, Bitmap.Config.RGB_565);

       /* new Thread(){
            @Override
            public void run() {

                String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+ File.separator+"image.jpg";

                Bitmap bitmap = BitmapFactory.decodeFile(file);
                Log.e(TAG,"bitmap;"+bitmap.getWidth()+"--"+bitmap.getHeight());
                final Bitmap compressImage = compressImage(bitmap);
                Log.e(TAG,"compressImage;"+compressImage.getWidth()+"--"+compressImage.getHeight());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mImage.setImageBitmap(compressImage);
                    }
                });
            }
        }.start();*/

        /*new Thread(){
            @Override
            public void run() {
                String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+ File.separator+"image.jpg";

                final Bitmap bitmap = reSizeBitmap(file, 480f, 800f, Bitmap.Config.ALPHA_8);
                Log.e("TAG","bitmap;"+bitmap.getWidth()+"--"+bitmap.getHeight());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mImage.setImageBitmap(bitmap);
                    }
                });
            }
        }.start();
*/
    }

/*
    public void claerImage(View view) {
        //清理图片
        BitmapDrawable drawable = (BitmapDrawable) mImage.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        mImage.setImageBitmap(null);
        bitmap.recycle();
        System.gc();
    }*/

    //图像二次采样
    //第一次采样
    //获取我们的图片的宽高
    //进行质量压缩  比例压缩
    //第二次采样  转换成我们压缩过后的图片


    /**
     * @param maxHeight 最大高度
     * @param maxWidth  最大宽度
     * @param config    图像配置
     * @return
     */
    /*private Bitmap createThumbnail(int res, int maxHeight, int maxWidth, Bitmap.Config config) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        //第一次采样，设置为true只加载边界数据，也就是获取图像的宽高
        options.inJustDecodeBounds = true;
        //通过bitmapfactory 解码数据 获取我们的图像宽高 ，宽高将保存到 options 中 此处不需要返回Bitmap 因为这个Bitmap为NULL；
        BitmapFactory.decodeResource(getResources(), res, options);
        int outWidth = options.outWidth;
        int outHeight = options.outHeight;
        Log.e("TAG", "bitmap:outWidth->" + outWidth + " bitmap:outHeight->" + outHeight);
        //质量压缩，压缩成之前质量的1/5   1/10  1/100
        int ratioWidth = 0;
        int ratioHeight = 0;
        if (maxHeight == 0 && maxWidth == 0) {
            options.inSampleSize = 1;
        } else if (maxHeight == 0 && maxWidth != 0) {
            //2880  /   1440   inSampleSize = 2
            ratioWidth = outWidth / maxWidth;
            options.inSampleSize = ratioWidth;
        } else if (maxHeight != 0 && maxWidth == 0) {
            ratioHeight = outHeight / maxHeight;
            options.inSampleSize = ratioHeight;
        } else {
            ratioWidth = outWidth / maxWidth;
            ratioHeight = outHeight / maxHeight;
            options.inSampleSize = (ratioWidth > ratioHeight ? ratioHeight : ratioWidth);
        }
        options.inPreferredConfig = config;
        //准备进行第二次采样，我们不仅仅加载边界数据
        options.inJustDecodeBounds = false;

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), res, options);
        Log.e("TAG", "bitmap:width->" + bitmap.getWidth() + " bitmap:height->" + bitmap.getHeight() + " inSampleSize:" + options.inSampleSize);
        return bitmap;
    }*/


    /**
     *
     * @param srcPath
     * @param maxHeight  定义的图片宽度
     * @param maxWidth   定义的图片高度
     * @return
     */
    public static Bitmap reSizeBitmap(String srcPath,float maxHeight,float maxWidth) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        //开始读入图片，此时把options.inJustDecodeBounds 设回true了
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此时返回bm为空
        int outWidth = newOpts.outWidth;
        int outHeight = newOpts.outHeight;
        Log.e("TAG","bitmap:outWidth"+outWidth+"--outHeight:"+outHeight);
        //现在主流手机比较多是800*480分辨率，所以高和宽我们设置为
        float height = maxHeight;//这里设置高度为800f
        float width = maxWidth;//这里设置宽度为480f
        //缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
        int be = 1;//be=1表示不缩放
        if (outWidth > outHeight && outWidth > width) {//如果宽度大的话根据宽度固定大小缩放
            be = (int) (newOpts.outWidth / width);
            Log.e("TAG","outWidth > outHeight :be"+be);
        } else if (outWidth < outHeight && outHeight > height) {//如果高度高的话根据宽度固定大小缩放
            be = (int) (newOpts.outHeight / height);
            Log.e("TAG","outWidth < outHeight :be"+be);
        }
        if (be <= 0){
            be = 1;
        }
        newOpts.inSampleSize = be;//设置缩放比例  靠近最小的一个 2的N次幂
        newOpts.inPreferredConfig = Bitmap.Config.ALPHA_8; //配置图片格式
        newOpts.inJustDecodeBounds = false;
        //重新读入图片，注意此时已经把options.inJustDecodeBounds 设回false了
        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
//        return bitmap;
        return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
    }

    /**
     * 质量压缩
     * @param image
     * @return
     */
    public static Bitmap compressImage(Bitmap image) {
        if (image != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
            int options = 100;
            while ( baos.toByteArray().length / 1024>100) {//循环判断如果压缩后图片是否大于100kb,大于继续压缩
                baos.reset();//重置baos即清空baos
                image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%，把压缩后的数据存放到baos中
                options -= 10;//每次都减少10
                if(options<=0){
                    options = 1;
                    image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%，把压缩后的数据存放到baos中
                    break;
                }
            }
            ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
            Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
            return bitmap;
        }
        return null;
    }
}
